import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.tree.*;

final class CloseItem extends AbstractAction{
  private final Doer frame;
  CloseItem(String name,Doer frame){
    super(name);
    this.frame=frame;
    putValue(SHORT_DESCRIPTION,name);
    String iconFileName="images"+File.separator+name+".gif";
    if(new File(iconFileName).exists())putValue(SMALL_ICON,new ImageIcon(iconFileName));
  }
  public void actionPerformed(ActionEvent e){
    closeFile();
  }
  public void closeFile(){
    if(frame.getTab().getTabCount()>0){
      InterWindow interWindow=(InterWindow)(frame.getTab().getComponentAt(frame.getSelect()));
      if(interWindow.isSaved()==false){
        int flag=JOptionPane.showConfirmDialog(frame,frame.getTab().getTitleAt(frame.getSelect())+" does'nt save.Save?",
								 "File does'nt save",
	                                                         JOptionPane.YES_NO_CANCEL_OPTION,
		                                                 JOptionPane.WARNING_MESSAGE);
        if(flag==JOptionPane.CANCEL_OPTION)return;
        else if(flag==JOptionPane.YES_OPTION)frame.getSaveItem().saveFile();
      }

      frame.getFileTree().getRoot().remove(frame.getSelect());
      frame.getFileTree().getTree().updateUI();
      frame.getTab().remove(frame.getSelect());
      if(frame.getSelect()>=0){
        InterWindow interWindow2=(InterWindow)(frame.getTab().getComponentAt(frame.getSelect()));
        frame.setTitle(frame.getTab().getTitleAt(frame.getSelect())+(interWindow2.isSaved()==true?' ':'*'));
      }
      if(frame.getTab().getTabCount()<1){
	frame.getSaveItem().setEnabled(false);
        frame.getSaveAsItem().setEnabled(false);
	frame.getSaveAllItem().setEnabled(false);
	frame.getCloseItem().setEnabled(false);
	frame.getCloseAllItem().setEnabled(false);
	frame.getFindItem().setEnabled(false);
	frame.getFindNextItem().setEnabled(false);
	frame.getCompileItem().setEnabled(false);
	frame.getRunItem().setEnabled(false);
	frame.getCopyItem().setEnabled(false);
	frame.getCutItem().setEnabled(false);
	frame.getPasteItem().setEnabled(false);
	frame.getUndoItem().setEnabled(false);
	frame.getRedoItem().setEnabled(false);
	frame.getStatusBar().getCD().setText("cd: the current file's dir");
	frame.getStatusBar().getLabel().setText("L: current line");
	frame.getCMDInfo().getTextArea().setText("");
	frame.setTitle("Doer");
      }
    }
  }
}
